import Vue from "vue";
import Vuex from "vuex";
import { ls } from "@/common/ls";
import { isEmpty, isEmptyObj } from "@/common/helps";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    device: "pc",
    userInfo: null,
    role: ""
  },
  mutations: {
    SET_DIVICE(state, data) {
      state.device = data;
      ls.set("device", data);
    },

  },
  actions: {

    setUserInfo({ commit }, userInfo) {
      commit("SET_USER_INFO", userInfo);
    }
  },
  getters: {

    getUserInfo: state => () => {
      let info = null;
      if (
        state.userInfo &&
        !isEmpty(state.userInfo) &&
        !isEmptyObj(state.userInfo)
      ) {
        info = state.userInfo;
      } else {
        info = ls.get("userInfo");
      }
      if (!info || isEmpty(info) || isEmptyObj(info)) {
        return {};
      }

      return info;
    }
  },
  modules: {}
});
